<?php
/**
 * Created by PhpStorm.
 * User: woody
 * Date: 17-2-16
 * Time: 下午10:46
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Admin\AdminController;
use Illuminate\Http\Request;

use App\Models\Msg;
use App\Models\User;

use Validator;

class MsgController extends AdminController {

    private $_length = 20;

    public function __construct()
    {
        parent::__construct();
        $this->_response['_active']['_model']       = 'msg';
        $this->_response['_active']['_action']      = '';
        $this->_response['_title']                  = '消息管理';
    }


    public function getList(Request $request){
        $page = 1;
        $search = [];
        $param = '';

        if($request->has('page')){
            $page = $request->get('page');
        }

        $offset = ($page - 1) * $this->_length;

        $userModel = new User();
        $msgModel = new Msg();
        
        $msg = Msg::select(
            $userModel->getTable().'.nick_name',
            $userModel->getTable().'.headimgurl',
            $userModel->getTable().'.wx_openid',
            $msgModel->getTable().'.id',
            $msgModel->getTable().'.time',
            $msgModel->getTable().'.address',
            $msgModel->getTable().'.num',
            $msgModel->getTable().'.img',
            $msgModel->getTable().'.remark',
            $msgModel->getTable().'.created_at'
        )->leftJoin($userModel->getTable() , $userModel->getTable() .'.id' , '=' , $msgModel->getTable().'.u_id')
            ->whereNull($msgModel->getTable().'.deleted_at')->orderBy($msgModel->getTable().'.created_at' , 'desc')
            ->skip($offset)->take($this->_length)->get();

        foreach ($msg as $m){
            $m->img = explode('|' , $m->img);
        }

        $count = Msg::whereNull('deleted_at')->count();

        $lastPage = ceil($count / $this->_length);

        $this->_response['msg']['list'] = $msg;
        $this->_response['msg']['pageData']['count'] = $count;
        $this->_response['msg']['pageData']['lastPage'] = $lastPage;
        $this->_response['msg']['pageData']['page'] = $page;
        $this->_response['msg']['pageData']['pageHtml'] = self::getPageHtml($page , $lastPage , '/admin/msg?' . $param);

        return view('admin.msg.list' , $this->_response);
    }


    public function delete($gId){
        if(Msg::where('id' , $gId)->update([
            'deleted_at' => date('Y-m-d H:i:s' , $_SERVER['REQUEST_TIME'])
        ])){
            return response()->json(['code' => 0 , 'msg' => '删除成功']);
        }else{
            return response()->json(['code' => -1 , 'msg' => '删除失败,请稍后重试']);
        }
    }


    public function userList(Request $request){
        $page = 1;
        $search = [];
        $param = '';

        if($request->has('page')){
            $page = $request->get('page');
        }

        $offset = ($page - 1) * $this->_length;

        $user = User::whereNull('deleted_at')->skip($offset)->take($this->_length)->get();

        $count = User::whereNull('deleted_at')->count();

        $lastPage = ceil($count / $this->_length);

        $this->_response['user']['list'] = $user;
        $this->_response['user']['pageData']['count'] = $count;
        $this->_response['user']['pageData']['lastPage'] = $lastPage;
        $this->_response['user']['pageData']['page'] = $page;
        $this->_response['user']['pageData']['pageHtml'] = self::getPageHtml($page , $lastPage , '/admin/user?' . $param);

        return view('admin.user.list' , $this->_response);
    }

}